package com.widget;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.landmesure.bluetoothchat.R;

import java.util.ArrayList;

public class SpinnerAdapter extends BaseAdapter implements SelectCallBack {
    private LayoutInflater mInflater;
    private ArrayList<String> mValues;
    private int mSelectPosition;

    SpinnerAdapter(LayoutInflater inflater, ArrayList<String> values, int selection) {
        mInflater = inflater;
        mValues = values;
        mSelectPosition = selection;
    }

    @Override
    public int getCount() {
        return mValues.size();
    }

    @Override
    public Object getItem(int position) {
        if (position < 0 || position >= getCount()) {
            return null;
        }
        return mValues.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.spinner_item, null);
            viewHolder = new ViewHolder();
            viewHolder.textView = convertView.findViewById(R.id.spinner_item_tv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (mSelectPosition == position) {
            viewHolder.textView.setBackgroundColor(Color.BLUE);
            viewHolder.textView.setTextColor(Color.WHITE);
        } else {
            viewHolder.textView.setBackgroundColor(Color.WHITE);
            viewHolder.textView.setTextColor(Color.GRAY);
        }
        viewHolder.textView.setText((String) getItem(position));

        return convertView;
    }

    @Override
    public void setSelectItem(int position) {
        mSelectPosition = position;
    }

    public void setValues(ArrayList<String> mValues) {
        this.mValues = mValues;
    }

    private class ViewHolder {
        TextView textView;
    }
}
